<%@page import="com.spreedairy.util.CategoryUtil"%>
<%@ include file="/html/common/init.jsp" %>

<style type="text/css">
	li {
		list-style:none;
	}
	a {
		text-decoration:none;
		color : #000;
	}
</style>
	
<portlet:renderURL var="addCategoriesURL">
	<portlet:param name="jspPage" value="/html/category/add_category.jsp" />
</portlet:renderURL>

<liferay-ui:header title="All Categories"/>
	
<%
	ShoppingCategory category = (ShoppingCategory)request.getAttribute("SHOPPING_CATEGORY");
	
	long categoryId = BeanParamUtil.getLong(category, request, "categoryId", ShoppingCategoryConstants.DEFAULT_PARENT_CATEGORY_ID);
	
	PortletURL portletURL = renderResponse.createRenderURL();
	portletURL.setParameter("categoryId", String.valueOf(categoryId));
	
	PortletURL editURL = renderResponse.createRenderURL();
	editURL.setParameter("jspPage", "/html/category/define_field.jsp");
	
	String plns = "";
	if (renderRequest != null) {
		plns = renderResponse.getNamespace();
	}
	
	
%>

<liferay-portlet:renderURL varImpl="searchURL">
	<portlet:param name="jspPage" value="/html/category/edit_category.jsp" />
</liferay-portlet:renderURL>

<portlet:actionURL name="updateProcess" var="actionURL">
	<portlet:param name="<%= Constants.CMD %>" value="<%= Constants.DELETE %>" />
</portlet:actionURL>


<aui:form action="<%= searchURL %>" method="post" name="fm">
		<liferay-portlet:renderURLParams varImpl="searchURL" />
		<aui:input name="redirect" type="hidden" value="<%= currentURL %>" />
		<aui:input name="breadcrumbsCategoryId" type="hidden" value="<%= categoryId %>" />
		<aui:input name="searchCategoryIds" type="hidden" value="<%= categoryId %>" />
	   <aui:input name="<%= Constants.CMD %>" type="hidden" />
		<%
		int total=0;
		List<ShoppingCategory> results=new ArrayList<ShoppingCategory>();
		
		List<String> headerNames = new ArrayList<String>();

		headerNames.add("category");
		headerNames.add("description");
		headerNames.add("categories");
		headerNames.add("items");
		headerNames.add("All Sub Categories");
		headerNames.add(StringPool.BLANK);
		String checkBox1 = "<input type='checkbox' name='" + plns + "check' onClick='" + plns + "checkAll(this);'/>";
		
		headerNames.add(checkBox1);
		PortletURL detailURL = renderResponse.createRenderURL();
		detailURL.setParameter("jspPage","/html/category/detail_category.jsp");

		SearchContainer searchContainer = new SearchContainer(renderRequest, null, null, "cur1", SearchContainer.DEFAULT_DELTA, portletURL, headerNames, null);
        
		PortletSession psession=renderRequest.getPortletSession();
		List<ShoppingCategory> searchorderlist=(List<ShoppingCategory>)psession.getAttribute("ADVANCE_SEARCH_PROJECT_LIST");
		System.out.println("searchorderlist>>>>>>>>>>"+searchorderlist);
		if(Validator.isNotNull(searchorderlist)){
			total=searchorderlist.size();
			results=searchorderlist;
			
		}
		else{
			total = ShoppingCategoryLocalServiceUtil.getCategoriesCount(scopeGroupId, categoryId);
			results = ShoppingCategoryLocalServiceUtil.getCategories(scopeGroupId, categoryId, searchContainer.getStart(), searchContainer.getEnd());
		}
	
		searchContainer.setTotal(total);

		searchContainer.setResults(results);

		List resultRows = searchContainer.getResultRows();
		for (int i = 0; i < results.size(); i++) {
			ShoppingCategory curCategory = (ShoppingCategory)results.get(i);

			curCategory = curCategory.toEscapedModel();

			ResultRow row = new ResultRow(curCategory, curCategory.getCategoryId(), i);

			PortletURL rowURL = renderResponse.createRenderURL();

			rowURL.setParameter("categoryId", String.valueOf(curCategory.getCategoryId()));
			rowURL.setParameter("jspPage","/html/category/detail_category.jsp");
			// Name and description
			
			row.addText(curCategory.getName(), rowURL);
			if (Validator.isNotNull(curCategory.getDescription())) {
				row.addText(curCategory.getDescription(), rowURL);
			}
			else {
				row.addText("No Description", rowURL);
			}

			// Statistics

			List subcategoryIds = new ArrayList();

			subcategoryIds.add(new Long(curCategory.getCategoryId()));

			ShoppingCategoryLocalServiceUtil.getSubcategoryIds(subcategoryIds, scopeGroupId, curCategory.getCategoryId());
			int categoriesCount = subcategoryIds.size() - 1;
			int itemsCount = ShoppingItemLocalServiceUtil.getCategoriesItemsCount(scopeGroupId, subcategoryIds);

			row.addText(String.valueOf(categoriesCount), rowURL);
			row.addText(String.valueOf(itemsCount), rowURL);
			if(categoriesCount>0){
			String showDiv = "<a href=\"javascript:void(0)\" onClick=\"showDiv('rowId"+curCategory.getCategoryId()+"');\">click</a>";
			row.addText(showDiv, rowURL);
			}
			else{
				row.addText("");
			}
			// Action
			//row.addJSP("right", SearchEntry.DEFAULT_VALIGN, "/html/portlet/shopping/category_action.jsp");
			// Add result row
			
			editURL.setParameter("categoryId", String.valueOf(curCategory.getCategoryId()));
			row.addText("EDIT",editURL);
			String checkBox2 = "<input type='checkbox' name='" + plns + "deleteItem' value='" + curCategory.getCategoryId() + "' onClick='" + plns + "checkAllRev(this);' />";
			row.addText(checkBox2);
			String recordId = String.valueOf(curCategory.getCategoryId());
			
			// Add result row

			resultRows.add(row);
			
			// Start ...........
			int count = ShoppingCategoryLocalServiceUtil.getCategoriesCount(scopeGroupId, curCategory.getCategoryId());
			if(count <= 0) continue;
			ResultRow row1 = new ResultRow(curCategory, curCategory.getCategoryId(), i);
			
			String subCategoryList = CategoryUtil.getSubCategoryList(scopeGroupId, curCategory.getCategoryId(), detailURL);
			row1.addText("left", "top", 6, subCategoryList);
			resultRows.add(row1);
			
			// End ..........
		}

		boolean showSearch = !results.isEmpty();
		psession.removeAttribute("ADVANCE_SEARCH_PROJECT_LIST");
		%>

		<liferay-ui:panel-container extended="<%= true %>" id="shoppingCategoriesPanelContainer" persistState="<%= true %>">
					<aui:fieldset>
						<c:if test="<%= showSearch %>">
							<aui:input id="keywords1" label="search" name="keywords" size="30" type="text" />
						</c:if>
					</aui:fieldset>

					<aui:button-row>
						<c:if test="<%= showSearch %>">
							<aui:button type="button" value="search-categories" onClick="javascript:categorySearch();"/>
						</c:if>
						<a href="<%=addCategoriesURL.toString()%>"><aui:button type="button" value="ADD Category"/></a>
						<c:if test="<%= showSearch %>">
							<input type="button" value="Delete" onClick="<%= plns %>confirmDel('delmultiple');"/>
						</c:if>
					</aui:button-row>

				<liferay-ui:search-iterator searchContainer="<%= searchContainer %>" />
		</liferay-ui:panel-container>
	</aui:form>

<aui:script>
function categorySearch(){
				document.<portlet:namespace />fm.<portlet:namespace /><%= Constants.CMD %>.value = "<%= Constants.SEARCH %>";
				submitForm(document.<portlet:namespace />fm, "<portlet:actionURL name="updateProcess"><portlet:param name="redirect" value="<%= currentURL %>" /></portlet:actionURL>");
}
		
</aui:script>

	<aui:script>
		function showDiv(id) {
			document.getElementById(id).style.display = "block";
		}
	</aui:script>
	
<script type="text/javascript">
	function <portlet:namespace/>checkAll(obj) {
		var items = document.getElementsByName("<portlet:namespace/>deleteItem");
		for (var i=0; i<items.length; i++) {
			items[i].checked = obj.checked;
		}
	}	
	function <portlet:namespace/>confirmDel(param) {
		msg = "Are you sure to delete the record?";
		if(param == "delmultiple") {
			if(<portlet:namespace/>getCheckedCount()==0){
				alert("No Items are selected !");
			} else {
				msg = "Are you sure to delete the record(s)?";
				if(confirm(msg)) {
					document.<portlet:namespace/>fm.action="<%= actionURL.toString()%>";
					document.<portlet:namespace/>fm.submit();
				}
			}
		} 
	}
	function <portlet:namespace/>checkAllRev() {
		var items = document.getElementsByName("<portlet:namespace/>deleteItem");
		size = items.length;
		if(size == <portlet:namespace/>getCheckedCount()) {
			document.<portlet:namespace/>fm.<portlet:namespace/>check.checked = true;
		} else {
			document.<portlet:namespace/>fm.<portlet:namespace/>check.checked = false;
		}
	}
	function <portlet:namespace/>getCheckedCount() {
		var items = document.getElementsByName("<portlet:namespace/>deleteItem");
		checkedCount=0;
		for (i=0; i< items.length; i++)
		{
	        if(items[i].checked)
	        {
	        	checkedCount += 1;
	        }
		}
		return checkedCount;
	}
</script>
